package com.prj.ufdm.webflux.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamMapTest {

	public static void main(String[] args) {
		
		testMap();
		
		testFlatMap();
        
	}
	
	/**
	 * DESC: Map 操作
	 */
	static void testMap() {
		
		List<Map<String, String>> list = new ArrayList<>();

		Map<String, String> map1 = new HashMap<>();
		map1.put("id", "101");
		map1.put("name", "Tomcat");

		Map<String, String> map2 = new HashMap<>();
		map2.put("id", "102");
		map2.put("name", "Apache");

		Map<String, String> map3 = new HashMap<>();
		map3.put("id", "103");
		map3.put("name", "Nginx");

		list.add(map1);
		list.add(map2);
		list.add(map3);

		List<String> ids1 = list.stream().map(entity -> entity.get("id")).collect(Collectors.toList());
		System.out.println("\n ids1:"+ids1);
		
		List<Object> ids2 = Arrays.asList(list.stream().map(entity -> entity.get("id")).toArray());
		System.out.println("\n ids2:"+ids2);
	}
	
	/**
	 * DESC: 对给定单词列表 ["Hello","World"],你想返回列表["H","e","l","o","W","r","d"]
	 */
	static void testFlatMap() {
		
		String[] words = new String[]{"Hello","World","Hello"};
        List<String> a = Arrays.stream(words)
                .map(word -> word.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        a.forEach(System.out::print);
	}
}
